﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GISDataAdapter.Data;
using System.Data.SqlClient;

namespace TestApp
{
    public partial class Form1 : Form
    {
        private int operateType = -1;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnLoadFile_Click(object sender, EventArgs e)
        {
            operateType = 0;
            openFileDialog1.FileName = String.Empty;
            openFileDialog1.Filter = "shp file (*.shp) |*.shp";
            openFileDialog1.ShowDialog();
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
            switch (operateType)
            {
                //Read shp files
                case 0:

                    if (dataGridView1.DataSource != null)
                    {
                        dataGridView1.DataSource = null;
                    }

                    List<string> fileNameList = new List<string>();
                    List<DataTable> resultList = new List<DataTable>();

                    foreach (string fileName in openFileDialog1.FileNames)
                    {
                        fileNameList.Add(fileName);
                    }

                    ShpFileLoader loader = new ShpFileLoader();
                    resultList = loader.Load(fileNameList);

                    dataGridView1.DataSource = resultList[0];

                    break;

                //Read dbf files
                case 1:

                    if (dataGridView1.DataSource != null)
                    {
                        dataGridView1.DataSource = null;
                    }

                    List<string> fileNameList1 = new List<string>();
                    List<DataTable> resultList1 = new List<DataTable>();

                    foreach (string fileName in openFileDialog1.FileNames)
                    {
                        fileNameList1.Add(fileName);
                    }

                    DbfFileLoader dbfLoader = new DbfFileLoader();
                    resultList1 = dbfLoader.Load(fileNameList1);

                    dataGridView1.DataSource = resultList1[0];
                    break;
                default:
                    break;
            }

            operateType = -1;
        }

        private void btnLoadData_Click(object sender, EventArgs e)
        {
            operateType = 1;
            openFileDialog1.Filter = "dbf files (*.dbf)|*.dbf";
            openFileDialog1.FileName = String.Empty;
            openFileDialog1.ShowDialog();
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            //SqlConnection conn = new SqlConnection();
            //conn.ConnectionString = "Data Source=localhost;Initial Catalog=ChinaDB;Persist Security Info=True;User ID=sa;Password=123456";
            //string strSql = String.Format
        }
    }
}